package com.cymjoe.testflutternativedemo

import android.content.Context
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor

class FlutterBuilder private constructor(context: Context) {
    var flutterEngine: FlutterEngine = FlutterEngine(context)
    var flutterEngine2: FlutterEngine = FlutterEngine(context)
    var flutterEngine3: FlutterEngine = FlutterEngine(context)

    companion object {
        @Volatile
        private var INSTANCE: FlutterBuilder? = null
        fun getInstance(context: Context): FlutterBuilder? {
            if (null == INSTANCE) {
                synchronized(FlutterBuilder::class.java) {
                    if (null == INSTANCE) {
                        INSTANCE = FlutterBuilder(context)
                    }
                }
            }
            return INSTANCE
        }
    }

    init {
        //构造函数
        flutterEngine.navigationChannel.setInitialRoute("/risk1")
        flutterEngine2.navigationChannel.setInitialRoute("/risk2")
        flutterEngine3.navigationChannel.setInitialRoute("/risk3")
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        flutterEngine2.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        flutterEngine3.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        FlutterEngineCache
            .getInstance()
            .put("start1", flutterEngine)
        FlutterEngineCache
            .getInstance()
            .put("start2", flutterEngine2)
        FlutterEngineCache
            .getInstance()
            .put("start3", flutterEngine3)
    }
}